#include "Sock.h"
#include <Acceptor.h>

namespace muduo{
    void Acceptor::Handleread(){
        int newfd = accept(_socket.Fd(), nullptr, nullptr);
        if(newfd < 0){
            errorlog << "accept error";
            return;
        }
        if(_acceptcallback) _acceptcallback(newfd);
    }
    int Acceptor::CreateServer(int port){
        bool ret = _socket.CreateServer(38080);
        assert(ret == true);
        return _socket.Fd();
    }
    void Acceptor::Listen(){_channel.EnableRead();}
    void Acceptor::SetAcceptCallback(const AcceptCallback& ac){_acceptcallback=ac;}
    Acceptor::Acceptor(EventLoop* loop,int port):_loop(loop),_socket(CreateServer(port)),_channel(loop,_socket.Fd()){
        _channel.Set_read_callback(std::bind(&Acceptor::Handleread,this));
    }
}